﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AmazeUI.bll;
using AmazeUI.bll.Open;
using AmazeUI.bll.Open.WeixinPay;
using Ema.Filters;
using Ema.Models.XtModel;

namespace Ema.Controllers
{
    public class GoodsController : BaseController
    {
        private PayBll payBll = new PayBll();

        private GoodsBll goodsBll = new GoodsBll();

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            goodsBll.Dispose();
            payBll.Dispose();
        }
        /// <summary>
        /// 获取商品详情
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>

        public ActionResult G(int gid)
        {
            var model = goodsBll.GetModel(gid);
            return this.GetJsonDb(model);
        }

        /// <summary>
        /// 订单查询
        /// </summary>
        /// <returns></returns>
        [WxValidate]
        public ActionResult PayOrderQuery()
        {
            var pq = payBll.PayOrderQuery(this.GetAdmin.Id);
            return this.GetJsonDb(pq);
        }

        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="gid"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddOrder(int gid, SubXtAdder model)
        {
            if (this.ModelState.IsValid)
            {
                model.Xtuserid = this.GetAdmin.Id;
                base.Logset("wx.JsNotify", gid + "," + model.Xtuserid);
                var po = payBll.AddOrder(gid, this.GetIp, model, DateTime.Now.AddMinutes(15));
                if (po == null)
                {
                    return RedirectToAction("g", new { gid, ms = "订单无法提交" });
                }
                //锁定15分钟
                goodsBll.LockGoods(gid, DateTime.Now.AddMinutes(15));
                return RedirectToAction("pay", "wx", new { oid = po.Id });
            }
            return RedirectToAction("g", new { gid, ms = "信息有误" });
        }


        /// <summary>
        /// 通知返回
        /// </summary>
        /// <returns></returns>
        public ActionResult JsNotify()
        {
            var inputStream = this.Request.InputStream;

            var nm = WxNotifyPayLogModel.GetNotifyModel(inputStream);

            if (string.IsNullOrWhiteSpace(nm.err_code))
            {
                var b = payBll.SetNotify(nm, false);
                if (b)
                {
                    return this.Content(WxNotifyPayLogModel.retok);
                }
                else
                {

                    base.Logset("wx.JsNotify", "重复支付:" + nm.OrderNo);
                    return this.Content(WxNotifyPayLogModel.retno);
                }

            }

            var str = WxNotifyPayLogModel.GetNotify(inputStream);
            base.Logset("wx.JsNotify", str);
            return this.Content(WxNotifyPayLogModel.retno);

        }

        
        
        /// <summary>
        /// 商品无效
        /// </summary>
        /// <returns></returns>
        public ActionResult Sorry( )
        {
            return this.RedirectToAction("Sorry", "Error", new { ms = "商品无效" });
        }
    }
}
